package com.wins.filter;

import com.wins.utils.LogUtils;
import io.restassured.filter.Filter;
import io.restassured.filter.FilterContext;
import io.restassured.response.Response;
import io.restassured.specification.FilterableRequestSpecification;
import io.restassured.specification.FilterableResponseSpecification;

import static io.qameta.allure.Allure.addAttachment;


//RestAssured过滤器
//1.辅助日志输出打印
//2.为接口请求自动增加头部header信息，比如token
public class DemoFilter implements Filter {

    @Override
    public Response filter(FilterableRequestSpecification filterableRequestSpecification, FilterableResponseSpecification filterableResponseSpecification, FilterContext filterContext) {
        // 原始的请求信息
        // 添加一个头信息
        filterableRequestSpecification.header("name", "ad");
        // 设置基地址
        filterableRequestSpecification.baseUri("https://httpbin.ceshiren.com");
        // 请求信息
        String requests_info = filterableRequestSpecification.getMethod() + " "
                + filterableRequestSpecification.getURI()
                + "\n Request Body =>"
                + filterableRequestSpecification.getBody();

        // 真实发起请求,获取响应对象
        Response response = filterContext.next(filterableRequestSpecification, filterableResponseSpecification);
        // 响应信息
        String response_info =
                "\n Response Status => "
                        + response.getStatusCode()
                        + " " + response.getStatusLine()
                        + "\n Response Body => " + response.getBody().asPrettyString();
        // ==== 日志的配置： 需求，每个接口再调用过程中，自动打印日志信息
        LogUtils.logger.debug("接口请求"+requests_info);
        addAttachment("接口请求", requests_info);

        LogUtils.logger.debug("接口响应"+response_info);
        addAttachment("接口响应", response_info);

        return response;


    }
}
